//
// Created by 123 on 2024/3/6.
//计算后缀表达式
#include <iostream>

#include "../0.utils/Int_Stack.cpp"

using namespace std;
//后缀表达式的计算
int compute(string str);

//是否是符号
bool isOpe(char ch);

//计算
int calculate(int a, int b, char ch);


int main() {
    string str="5711+-/3*211++-";
    int ans= compute(str);
    cout<<"结果是"<<ans<<endl;

}

int compute(string str) {
    LinkStack stack = init_IntStack();
    for (int i = 0; i < str.length(); ++i) {
        if (!isOpe(str[i])) {
            int a=str[i]-48;
            push(stack, a);
        } else {
            int a,b;
            b=pop(stack);
            a=pop(stack);
            int ans=calculate(a,b,str[i]);
            push(stack,ans);
        }
    }
    return getTop(stack);
}
bool isOpe(char ch) {
    return ch == '+' || ch == '-' || ch == '*' || ch == '/';
}
int calculate(int a, int b, char ch) {
    if (ch == '+') {
        return a + b;
    } else if (ch == '-') {
        return a - b;
    } else if (ch == '*') {
        return a * b;
    } else {
        return a / b;
    }
}

int getOpeNum(char a) {
    if (a == '*' || a == '/')
        return 1;
    else
        return 0;
}

